package com.mall.controller;

import com.mall.domain.ItemCat;
import com.mall.domain.OrderCopy;
import com.mall.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {
	@Autowired
	private IOrderService orderService;

	@RequestMapping("/findAllOrder")
	public @ResponseBody Map<String,Object> findAllOrder(HttpServletRequest request){
		Map<String,Object> categoriesMap = new HashMap<>();
		//获取商品类别集合
		List<OrderCopy> categories = orderService.findAllOrder();
		System.out.println(categories);

		//放入Map集合
		categoriesMap.put("message",categories);
		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","获取商品类别列表成功");
		metaMap.put("status",200);

		categoriesMap.put("meta",metaMap);
		return categoriesMap;

	}


	@RequestMapping("/findAllOrderByType")
	public @ResponseBody
	Map<String,Object> findAllOrderByType(HttpServletRequest request){
		Integer type = Integer.parseInt(request.getParameter("type"));
		Map<String,Object> categoriesMap = new HashMap<>();
		//获取商品类别集合
		List<OrderCopy> categories = orderService.findAllOrderByType(type);
		//放入Map集合
		categoriesMap.put("message",categories);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","获取商品类别列表成功");
		metaMap.put("status",200);

		categoriesMap.put("meta",metaMap);
		return categoriesMap;
	}

	@RequestMapping("/findOrderById")
	public @ResponseBody
	Map<String,Object> findOrderById(HttpServletRequest request){
		String order_code = request.getParameter("order_code");
		Map<String,Object> ordersMap = new HashMap<>();
		//获取商品类别集合
		List<OrderCopy> categories = orderService.findOrderById(order_code);
		//放入Map集合
		ordersMap.put("message",categories);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","获取商品类别列表成功");
		metaMap.put("status",200);

		ordersMap.put("meta",metaMap);
		return ordersMap;
	};

	@RequestMapping("/deleteOrder")
	public @ResponseBody
	Map<String,Object> deleteOrder(HttpServletRequest request){
		String order_code = request.getParameter("order_code");
		Map<String,Object> ordersMap = new HashMap<>();
		//获取商品类别集合
		Integer categories = orderService.deleteOrder(order_code);
		//放入Map集合
		ordersMap.put("message",categories);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","删除订单成功");
		metaMap.put("status",200);

		ordersMap.put("meta",metaMap);
		return ordersMap;
	}

	// 支付订单：生成支付时间
	@RequestMapping("/payOrder")
	public @ResponseBody
	Map<String,Object> payOrder(HttpServletRequest request){
		Date payment_time1 = new Date();
		String order_code = request.getParameter("order_code");
		Map<String,Object> ordersMap = new HashMap<>();
		//获取商品类别集合
		Integer categories = orderService.payOrder(payment_time1,order_code);
		//放入Map集合
		ordersMap.put("message",categories);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","订单支付成功");
		metaMap.put("status",200);

		ordersMap.put("meta",metaMap);
		return ordersMap;
	}

	// 确认收货，完成订单：生成endTime时间
	@RequestMapping("/endOrder")
	public @ResponseBody
	Map<String,Object> endOrder(HttpServletRequest request){
		Date endTime = new Date();
		String order_code = request.getParameter("order_code");
		Map<String,Object> ordersMap = new HashMap<>();
		//获取商品类别集合
		Integer categories = orderService.endOrder(endTime,order_code);
		//放入Map集合
		ordersMap.put("message",categories);

		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","订单完成成功");
		metaMap.put("status",200);

		ordersMap.put("meta",metaMap);
		return ordersMap;
	}

	@RequestMapping("/findAllOrderDetails")
	public @ResponseBody Map<String,Object> findAllOrderDetails(HttpServletRequest request){
		Map<String,Object> categoriesMap = new HashMap<>();
		//获取商品类别集合
		List<OrderCopy> categories = orderService.findAllOrderDetails();
		System.out.println(categories);

		//放入Map集合
		categoriesMap.put("message",categories);
		//创建状态Map
		Map<String,Object> metaMap = new HashMap<>();
		metaMap.put("msg","获取商品类别列表成功");
		metaMap.put("status",200);

		categoriesMap.put("meta",metaMap);
		return categoriesMap;

	}

}
